package com.jackrain.nea.oms.mq;

import com.aliyun.openservices.ons.api.MessageListener;
import com.aliyun.openservices.ons.api.bean.ConsumerBean;
import com.aliyun.openservices.ons.api.bean.ProducerBean;
import com.aliyun.openservices.ons.api.bean.Subscription;
import com.jackrain.nea.config.PropertiesConf;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.InstanceUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.cglib.proxy.Callback;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by ZC on 2017/4/11.
 */
@Slf4j
@Component
@AutoConfigureOrder(98)//运行顺序 数越小越先初始化
public class StartupRunner implements CommandLineRunner {

    @Autowired
    private PropertiesConf pconf;

    @Autowired
    Environment env;

    private Map<Subscription,MessageListener> map = new HashMap<Subscription,MessageListener>();

    public StartupRunner() {
    }

    @Override
    public void run(String... args) throws Exception {
        //mq是否启动
        Boolean  producer = Boolean.valueOf(pconf.getProperty("ons.mq.producer.start"));
        Boolean  consumer = Boolean.valueOf(pconf.getProperty("ons.mq.consumer.start"));
        if(producer){
            ProducerBean producerBean = (ProducerBean) ApplicationContextHandle.getBean(ProducerBean.class);
            producerBean.start();
        }
        if(consumer){
            initSubscription();
            ConsumerBean consumerBean = (ConsumerBean) ApplicationContextHandle.getBean("consumerBean");
            consumerBean.setSubscriptionTable(map);
            consumerBean.start();
        }
    }

    /**
     * 初始化订阅关系
     */
    private void initSubscription() throws Exception {
        String dsPrefixs = pconf.getProperty("ons.mq.index");
        /*for (String dsPrefix : dsPrefixs.split(",")) {// 多个数据源
            String listenerC=pconf.getProperty("ons.mq.listenerC"+dsPrefix);
            Class classz = InstanceUtil.getClass(listenerC);
            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(classz);
            enhancer.setCallback(new MQLogIntercept());
            MessageListener messageListener = (MessageListener) enhancer.create();
            String topic=pconf.getProperty("ons.mq.topic"+dsPrefix);
            String expr=pconf.getProperty("ons.mq.expr"+dsPrefix,"*");
            Subscription subscription = new Subscription();
            subscription.setTopic(topic);
            subscription.setExpression(expr);
            map.put(subscription,messageListener);
        }*/
        String logIntercept = this.pconf.getProperty("ons.mq.consumer.logIntercept");
        String[] var3 = dsPrefixs.split(",");
        int var4 = var3.length;

        for(int var5 = 0; var5 < var4; ++var5) {
            String dsPrefix = var3[var5];
            String listenerC = this.pconf.getProperty("ons.mq.listenerC" + dsPrefix);
            String topic = this.pconf.getProperty("ons.mq.topic" + dsPrefix);
            String expr = this.pconf.getProperty("ons.mq.expr" + dsPrefix, "*");
            if(listenerC == null) {
                log.error("注意：Topic" + topic + "无监听程序");
                return;
            }

            Class classz = InstanceUtil.getClass(listenerC);
            if(logIntercept != null) {
                Class messageListener = InstanceUtil.getClass(logIntercept);
                Enhancer subscription = new Enhancer();
                subscription.setSuperclass(classz);
                subscription.setCallback((Callback)messageListener.newInstance());
                Object o = subscription.create();
                if(o instanceof MessageListener) {
                    MessageListener messageListener1 = (MessageListener)o;
                    Subscription subscription1 = new Subscription();
                    subscription1.setTopic(topic);
                    subscription1.setExpression(expr);
                    this.map.put(subscription1, messageListener1);
                } else {
                    log.error("MQ普通消息消费监听程序类型错误，参数ons.mq.topic" + dsPrefix);
                }
            } else {
                MessageListener var16 = (MessageListener)classz.newInstance();
                Subscription var17 = new Subscription();
                var17.setTopic(topic);
                var17.setExpression(expr);
                this.map.put(var17, var16);
            }
        }
    }





}
